package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.impl.impl.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * spring定时器
 */
@Component
public class HotDataPersistenceJob {

    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    //从0秒开始,每5秒执行一次
    @Scheduled(cron = "0/5 * * * * ? ")
    public void TestTask() {
        System.out.println("spring定时器执行任务中....");

        //1:把数据从redis中查询出来
        List<StrategyStats> list = strategyStatsCacheService.listStats();
        for (StrategyStats stats : list) {
            //2:把数据封装成detail对象
            StrategyDetail detail = new StrategyDetail();
            detail.setId(stats.getStrategyId());
            detail.setViewnum(stats.getViewnum());
            detail.setReplynum(stats.getReplynum());
            detail.setFavornum(stats.getFavornum());
            detail.setThumbsupnum(stats.getThumbsupnum());
            // 3:更新到mysql中
            strategyDetailService.updateStats(detail);
        }

    }
}
